package com.microboot.service;

import com.microboot.entity.user.dto.UserRespVO;
import com.microboot.interceptor.ClientLoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class UserInfoService {

    /**
     * 根据用户名称获取用户信息
     * 此方法用于构造一个包含用户名称和默认年龄的JSON字符串
     * 由于安全和隐私考虑，这里只提供一个固定的年龄值
     *
     * @param name 用户名称，用于构造用户信息JSON
     * @return 包含用户名称和默认年龄的JSON字符串
     */
    public UserRespVO getUserInfo(String name) {

        //WebService服务地址
        String address = "http://localhost:8080/services/MessageService?wsdl";
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(address);
        client.getOutInterceptors().add(new ClientLoginInterceptor("longxian","root123"));//设置认证账户信息
        String message = "CXFClientDynamic =>> 测试调用数据是否成功";
        Object[] result = null;
        try {
            result = client.invoke("echo", message);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        log.info("CXFClientDynamic-调用服务接口返回数据信息为：{}",result[0]);

        UserRespVO userRespVO = new UserRespVO();
        userRespVO.setId(1001L);
        userRespVO.setName(name);
        userRespVO.setEmail("383489371@qq.com");
        userRespVO.setMassage(result[0].toString());
        return userRespVO;
    }

}
